package com.ksd.control;

import com.alibaba.fastjson.JSONObject;
import com.ksd.control.result.LoginResult;
import com.ksd.control.result.ResultParam;
import com.ksd.pojo.User;
import com.ksd.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

@Controller
public class LoginAction extends BaseAction {
    @Autowired
    private UserService userService;

    private String loginJson;

    public String getLoginJson() {
        return loginJson;
    }

    public void setLoginJson(String loginJson) {
        this.loginJson = loginJson;
    }

    @Override
    public String execute() {
        User user = JSONObject.parseObject(loginJson, User.class);
        if (userService.verifyLogin(user)) {
            user = userService.getUserMessage(user.getUsername());
            LoginResult loginResult = new LoginResult();
            loginResult.setId(String.valueOf(user.getId())).setUsername(user.getUsername()).setResult(ResultParam.SUCCESS);
            this.setResult(loginResult);

            map.put("user", user);
        }
        return SUCCESS;
    }

}
